package com.jsyh.maidu.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.GoodsInfoActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.model.Cuxiao;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.jsyh.maidu.utils.TimeUtils.setTime;

/**
 * Created by ZHY_9 on 2017/7/14.
 */

public class HomeSaleAdapter extends RecyclerView.Adapter<HomeSaleAdapter.ScaleHolder> {
    List<Cuxiao> data;
    Context context;

    public HomeSaleAdapter(List<Cuxiao> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public ScaleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_sale, parent, false);
        ScaleHolder holder = new ScaleHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ScaleHolder holder, final int position) {
        Picasso.with(context).load(ConfigValue.IMG_IP + data.get(position).getGoods_thumb()).into(holder.goodsImg);
        holder.goodsName.setText(data.get(position).getName());
        holder.goodsPrice.setText(data.get(position).getPromote_price());
        String time = data.get(position).getDaojishi();
        long timeInt = Long.valueOf(time) * 1000;
        new CountDownTimer(timeInt, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                holder.saleTime.setText(setTime(millisUntilFinished / 1000));
            }

            @Override
            public void onFinish() {

            }
        }.start();

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("goodsId", data.get(position).getId());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    class ScaleHolder extends RecyclerView.ViewHolder {

        ImageView goodsImg;
        TextView goodsName;
        TextView goodsPrice;
        TextView saleTime;
        View view;

        public ScaleHolder(View itemView) {
            super(itemView);
            view = itemView;
            goodsImg = (ImageView) itemView.findViewById(R.id.goods_img);
            goodsName = (TextView) itemView.findViewById(R.id.goods_name);
            goodsPrice = (TextView) itemView.findViewById(R.id.goods_price);
            saleTime = (TextView) itemView.findViewById(R.id.sale_time);
        }
    }

}
